/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmPatientHistory.java
 *
 * Arthur mbelisle age 24
 * Created on Jul 15, 2011, 5:05:38 PM
 */
package khmhappointmentsystem;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author mbelisle
 */
public class FrmPatientHistory extends javax.swing.JInternalFrame
{

    ArrayList<String> firstName = new ArrayList<String>();
    ArrayList<String> lastName = new ArrayList<String>();
    ArrayList<String> id = new ArrayList<String>();

    /** Creates new form FrmPatientHistory */
    public FrmPatientHistory()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtSearch = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblpatients = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtSSN = new javax.swing.JTextField();
        txtFirstName = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        cmdHistory = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(khmhappointmentsystem.Main.class).getContext().getResourceMap(FrmPatientHistory.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txtSearch.setText(resourceMap.getString("txtSearch.text")); // NOI18N
        txtSearch.setName("txtSearch"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(khmhappointmentsystem.Main.class).getContext().getActionMap(FrmPatientHistory.class, this);
        jButton1.setAction(actionMap.get("Search")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblpatients.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "SSN", "First Name", "Last Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblpatients.setName("tblpatients"); // NOI18N
        tblpatients.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblpatientsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblpatients);
        tblpatients.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tblpatients.columnModel.title0")); // NOI18N
        tblpatients.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tblpatients.columnModel.title1")); // NOI18N
        tblpatients.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tblpatients.columnModel.title2")); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txtSSN.setText(resourceMap.getString("txtSSN.text")); // NOI18N
        txtSSN.setName("txtSSN"); // NOI18N

        txtFirstName.setText(resourceMap.getString("txtFirstName.text")); // NOI18N
        txtFirstName.setName("txtFirstName"); // NOI18N

        txtLastName.setText(resourceMap.getString("txtLastName.text")); // NOI18N
        txtLastName.setName("txtLastName"); // NOI18N

        cmdHistory.setAction(actionMap.get("History")); // NOI18N
        cmdHistory.setIcon(resourceMap.getIcon("cmdHistory.icon")); // NOI18N
        cmdHistory.setText(resourceMap.getString("cmdHistory.text")); // NOI18N
        cmdHistory.setName("cmdHistory"); // NOI18N

        jButton2.setAction(actionMap.get("Exit")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtLastName))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtFirstName))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(37, 37, 37)
                                .addComponent(txtSSN, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(41, 41, 41)
                        .addComponent(cmdHistory)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2)
                    .addComponent(cmdHistory))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-482)/2, (screenSize.height-324)/2, 482, 324);
    }// </editor-fold>//GEN-END:initComponents

    private void tblpatientsMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tblpatientsMouseClicked
    {//GEN-HEADEREND:event_tblpatientsMouseClicked
        // TODO add your handling code here:
        String id_no = tblpatients.getValueAt(tblpatients.getSelectedRow(), 0).toString();
        loadPatientInfo(id_no);
    }//GEN-LAST:event_tblpatientsMouseClicked

    private void loadPatientInfo(String selected_id)
    {
        for (int i = 0; i < id.size(); i++)
        {
            String pt_ssn = id.get(i);
            if (selected_id.equals(pt_ssn))
            {
                txtSSN.setText(pt_ssn);
                txtFirstName.setText(firstName.get(i));
                txtLastName.setText(lastName.get(i));
            }
        }
    }

    @Action
    public void Search()
    {
        try
        {

            firstName = new ArrayList<String>();
            lastName = new ArrayList<String>();
            id = new ArrayList<String>();

            String query = "SELECT pt_id, pt_ftname, pt_ltname FROM Patient_Info WHERE (pt_ftname LIKE ? OR pt_ltname LIKE ?) ORDER BY pt_id, pt_ftname, pt_ltname";
            PreparedStatement pstmt = Environment.getDbConn().prepareStatement(query);
            pstmt.setString(1, Utilities.percent(txtSearch.getText().trim()));
            pstmt.setString(2, Utilities.percent(txtSearch.getText().trim()));

            ResultSet rs = pstmt.executeQuery();

            while (rs.next())
            {
                id.add(rs.getString("pt_id"));
                firstName.add(rs.getString("pt_ftname"));
                lastName.add(rs.getString("pt_ltname"));
            }

            if (firstName.isEmpty() == false)//If there were results in the query create a table model with the vectors.
            {
                DefaultTableModel model = new DefaultTableModel()
                {

                    @Override
                    public Class getColumnClass(int columnIndex)
                    {
                        Object o = getValueAt(0, columnIndex);
                        if (o == null)
                        {
                            return Object.class;
                        }
                        else
                        {
                            return o.getClass();
                        }
                    }
                };

                model.addColumn("ID", id.toArray());
                model.addColumn("First Name", firstName.toArray());
                model.addColumn("Last Name", lastName.toArray());
                tblpatients.setModel(model);
            }
            else //If the query returned no results create a table model that displays a "No Result" message and disables the view salaries register button.
            {
                DefaultTableModel model = new DefaultTableModel()
                {

                    @Override
                    public boolean isCellEditable(int row, int column)
                    {
                        return false;
                    }

                    @Override
                    public Class getColumnClass(int columnIndex)
                    {
                        Object o = getValueAt(0, columnIndex);
                        if (o == null)
                        {
                            return Object.class;
                        }
                        else
                        {
                            return o.getClass();
                        }
                    }
                };

                id.add("No Results");
                firstName.add("No Results");
                lastName.add("No Results");

                model.addColumn("ID", id.toArray());
                model.addColumn("First Name", firstName.toArray());
                model.addColumn("Last Name", lastName.toArray());
                tblpatients.setModel(model);
            }
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(rootPane, "No Appiontments exist for this specialist clinic.");
        }
    }

    @Action
    public void History()
    {
        String Ptid = txtSSN.getText().toString();

        String report = "Patient_History.jasper";
        String location = java.util.ResourceBundle.getBundle("khmhappointmentsystem.resources/Main").getString("reportFolderLocation");
        report = location + report;

        // Second, create a map of parameters to pass to the report.
        Map parameters = new HashMap();
        parameters.put("id", Ptid);

        try
        {
            ReportFill ReportFill = new ReportFill();
            ReportFill.generateReport(report, parameters, Environment.getDbConn(), "Patient Appointment History");
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }

    @Action
    public void Exit()
    {
        this.dispose();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdHistory;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblpatients;
    private javax.swing.JTextField txtFirstName;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtSSN;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables
}
